package com.example.common.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.example.common.dto.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BaseExceptionHandler {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    protected ResponseEntity<BaseResponse<Void>> handleException(Exception e, ErrorCode errorCode, HttpStatus httpStatus) {
        if (e instanceof BizException) {
            // 业务异常只记录基本信息，不需要堆栈
            logger.warn("业务异常 - 错误码: {}, 消息: {}", errorCode.getCode(), e.getMessage());
        } else {
            // 系统异常记录完整堆栈
            logger.error("系统异常 - 错误码: {}, 消息: {}", errorCode.getCode(), e.getMessage(), e);
        }
        
        return ResponseEntity
                .status(httpStatus)
                .body(new BaseResponse<>(errorCode.getCode(), errorCode.getMessage(), null));
    }
} 